package com.xms.security.service.dataauth;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.security.entity.EntityPermission;
import com.xms.utils.CollectionUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;
import java.util.UUID;

/**
 * <p>
 * EntityPermissionDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@CacheConfig(cacheNames = "entitypermission", keyGenerator = "CacheKeyGenerator")
@Service
public class EntityPermissionDeleterService extends DeleterService<EntityPermission> implements IEntityPermissionDeleterService {

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        return super.deleteById(id);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws SQLException, XmsDependencyException {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        super.deleteByIds(idList);

        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<EntityPermission> queryWrapper) throws SQLException {
        return super.remove(queryWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByEntityId(UUID entityid) throws SQLException {
        return this.deleteMany(new LambdaQueryWrapper<EntityPermission>().eq(EntityPermission::getEntityid, entityid));
    }
}
